
class TimeUtils {
  static String fromNow(longTime) {
    if (longTime == null) {
      return "";
    }
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(longTime);
    final _now = new DateTime.now();
    final _diff = _now.difference(dateTime);

    if (_diff.inMinutes < 1) {
      return '刚刚';
    } else if (_diff.inMinutes < 2) {
      return '${_diff.inMinutes}分钟前';
    } else if (_diff.inMinutes < 60) {
      return '${_diff.inMinutes}分钟前';
    } else if (_diff.inHours <= 24) {
      return '${_diff.inHours}小时前';
    } else {
      return '${_diff.inDays}天前';
    }
  }

  static String timeYearMontyDay(startTime, endTime) {
    //需要判断年数是否是今年
    //    2019.12.25
    if (startTime == null || endTime == null) {
      return "";
    }
    DateTime today = DateTime.now();
    var date1 = today.millisecondsSinceEpoch;
    DateTime date2 = DateTime.fromMillisecondsSinceEpoch(date1);
    //判断结束的年数是否和当前任务的年相同
    DateTime dateStartTime = DateTime.fromMillisecondsSinceEpoch(startTime);
    DateTime dateEndTime = DateTime.fromMillisecondsSinceEpoch(endTime);
    String startMonth, startDay, endMonth, endDay;

    if (dateStartTime.month.toString().length > 1) {
      startMonth = "${dateStartTime.month}";
    } else {
      startMonth = "0${dateStartTime.month}";
    }
    if (dateStartTime.day.toString().length > 1) {
      startDay = "${dateStartTime.day}";
    } else {
      startDay = "0${dateStartTime.day}";
    }

    if (dateEndTime.month.toString().length > 1) {
      endMonth = "${dateEndTime.month}";
    } else {
      endMonth = "0${dateEndTime.month}";
    }

    if (dateEndTime.day.toString().length > 1) {
      endDay = "${dateEndTime.day}";
    } else {
      endDay = "0${dateEndTime.day}";
    }
    if (dateEndTime.year == date2.year) {
      return "$startMonth.$startDay-$endMonth.$endDay";
    } else {
      return "${dateStartTime.year}.$startMonth.$startDay-${dateEndTime.year}.$endMonth.$endDay";
    }
  }

  static String timeYearMonty2Day(startTime, endTime) {
    //需要判断年数是否是今年
    //    2019.12.25
    if (startTime == null || endTime == null) {
      return "";
    }
    DateTime today = DateTime.now();
    var date1 = today.millisecondsSinceEpoch;
    DateTime date2 = DateTime.fromMillisecondsSinceEpoch(date1);
    //判断结束的年数是否和当前任务的年相同
    DateTime dateStartTime = DateTime.fromMillisecondsSinceEpoch(startTime);
    DateTime dateEndTime = DateTime.fromMillisecondsSinceEpoch(endTime);
    String startMonth,
        startDay,
        endMonth,
        endDay,
        startMinute,
        startHour,
        endMinute,
        endHour;

    if (dateStartTime.month.toString().length > 1) {
      startMonth = "${dateStartTime.month}";
    } else {
      startMonth = "0${dateStartTime.month}";
    }
    if (dateStartTime.day.toString().length > 1) {
      startDay = "${dateStartTime.day}";
    } else {
      startDay = "0${dateStartTime.day}";
    }
    if (dateStartTime.minute.toString().length > 1) {
      startMinute = "${dateStartTime.minute}";
    } else {
      startMinute = "0${dateStartTime.minute}";
    }
    if (dateStartTime.hour.toString().length > 1) {
      startHour = "${dateStartTime.hour}";
    } else {
      startHour = "0${dateStartTime.hour}";
    }
    if (dateEndTime.month.toString().length > 1) {
      endMonth = "${dateEndTime.month}";
    } else {
      endMonth = "0${dateEndTime.month}";
    }

    if (dateEndTime.day.toString().length > 1) {
      endDay = "${dateEndTime.day}";
    } else {
      endDay = "0${dateEndTime.day}";
    }
    if (dateEndTime.minute.toString().length > 1) {
      endMinute = "${dateEndTime.minute}";
    } else {
      endMinute = "0${dateEndTime.minute}";
    }
    if (dateEndTime.hour.toString().length > 1) {
      endHour = "${dateEndTime.hour}";
    } else {
      endHour = "0${dateEndTime.hour}";
    }

    if (dateEndTime.year == date2.year) {
      return "$startMonth.$startDay\t$startHour:$startMinute-$endMonth.$endDay\t$endHour:$endMinute";
    } else {
      return "${dateStartTime.year}.$startMonth.$startDay\t$startHour:$startMinute-${dateEndTime.year}.$endMonth.$endDay\t$endHour:$endMinute";
    }
  }

  static bool timeStartGuoQi(startTime) {
    //需要判断年数是否是今年
    //    2019.12.25
    if (startTime == null) {
      return false;
    }
    DateTime today = DateTime.now();
    print(today);
    var date1 = today.millisecondsSinceEpoch;
    if (date1 < startTime) {
      //表示开始
      return true;
    } else {
      //表示没有开始
      return false;
    }
  }

  static bool timeEndGuoQi(endTime) {
    //需要判断年数是否是今年
    //    2019.12.25
    if (endTime == null) {
      return false;
    }
    DateTime today = DateTime.now();
    print(today);
    var date1 = today.millisecondsSinceEpoch;
    if (date1 < endTime) {
      //不过期
      return true;
    } else {
      //过期
      return false;
    }
  }

  //解析时间戳
  static String getDateTime(longTime) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(longTime);
    return "${dateTime.year}-${dateTime.month}-${dateTime.day}";
  }

  static String timeActivityPage(startTime, endTime) {
    //需要判断是否是同一天
    if (startTime == null || endTime == null) {
      return "";
    }
    //判断结束的年数是否和当前任务的年相同
    DateTime dateStartTime = DateTime.fromMillisecondsSinceEpoch(startTime);
    DateTime dateEndTime = DateTime.fromMillisecondsSinceEpoch(endTime);
    String startMonth = dateStartTime.month.toString().padLeft(2, "0");
    String startDay = dateStartTime.day.toString().padLeft(2, "0");
    String startHour = dateStartTime.hour.toString().padLeft(2, "0");
    String startMinute = dateStartTime.minute.toString().padLeft(2, "0");
    String endMonth = dateEndTime.month.toString().padLeft(2, "0");
    String endDay = dateEndTime.day.toString().padLeft(2, "0");
    String endHour = dateEndTime.hour.toString().padLeft(2, "0");
    String endMinute = dateEndTime.minute.toString().padLeft(2, "0");
    if (dateStartTime.year == dateEndTime.year &&
        dateStartTime.month == dateEndTime.month &&
        dateStartTime.day == dateEndTime.day) {
      return "${dateStartTime.year}.$startMonth.$startDay\t$startHour:$startMinute-\t$endHour:$endMinute";
    } else {
      return "${dateStartTime.year}.$startMonth.$startDay\t$startHour:$startMinute-${dateEndTime.year}.$endMonth.$endDay\t$endHour:$endMinute";
    }
  }

  static String getCurrentPosition(int seconds) {
    String hours = '00';
    int timeHours = seconds ~/ (60 * 60);
    int timeMinutes = (seconds ~/ 60) - (timeHours * 60);
    int timeSeconds = seconds - (timeHours * 60 * 60) - (timeMinutes * 60);

    if (timeHours > 9) {
      hours = '$timeHours';
    } else if (timeHours > 0 && timeHours < 10) {
      hours = '0$timeHours';
    } else {
      hours = '00';
    }
    String minutes = '00';
    if (timeMinutes > 9) {
      minutes = '$timeMinutes';
    } else if (timeMinutes > 0 && timeMinutes < 10) {
      minutes = '0$timeMinutes';
    } else {
      minutes = '00';
    }
    String second = '00';
    if (timeSeconds > 9) {
      second = '$timeSeconds';
    } else if (timeSeconds > 0 && timeSeconds < 10) {
      second = '0$timeSeconds';
    } else {
      second = '00';
    }
    return '$hours:$minutes:$second';
  }

  static double getProgress(int seconds, int duration) {
    return seconds / duration;
  }

  static String getTimeList(int seconds) {
    if (seconds == 0) {
      return "";
    }

    //需要判断年数是否是今年
    DateTime today = DateTime.now();
    var date1 = today.millisecondsSinceEpoch;
    DateTime date2 = DateTime.fromMillisecondsSinceEpoch(date1);
    //判断结束的年数是否和当前任务的年相同
    DateTime dateStartTime = DateTime.fromMillisecondsSinceEpoch(seconds);
    String month, day, hour, second;
    month = dateStartTime.month.toString().padLeft(2, "0");
    day = dateStartTime.day.toString().padLeft(2, "0");
    hour = dateStartTime.hour.toString().padLeft(2, "0");
    second = dateStartTime.second.toString().padLeft(2, "0");

    if (dateStartTime.year == date2.year) {
      //月  日  时 分
      return "$month月$day日\t$hour:$second";
    } else {
      //年 月  日  时 分
      return "${dateStartTime.year}年$month月$day日\t$hour:$second";
    }
  }

  static String getTime(int seconds) {
    if (seconds == 0) {
      return "";
    }

    //需要判断年数是否是今年
//    DateTime today = DateTime.now();
//    var date1 = today.millisecondsSinceEpoch;
    //判断结束的年数是否和当前任务的年相同
    DateTime dateStartTime = DateTime.fromMillisecondsSinceEpoch(seconds);
    String month, day, hour, second;
    month = dateStartTime.month.toString().padLeft(2, "0");
    day = dateStartTime.day.toString().padLeft(2, "0");
    hour = dateStartTime.hour.toString().padLeft(2, "0");
    second = dateStartTime.second.toString().padLeft(2, "0");
    //年 月  日  时 分
    return "${dateStartTime.year}年$month月$day日\t$hour:$second";
  }

}
